package bd403.utils;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import bd403.Backend.Review;
import bd403.namespace.R;

/* Class that manages a list of reviews
 * Main author: Joe
 */

public class ReviewAdapter extends ArrayAdapter<Review>{
	private final Activity context;
	private ArrayList<Review> reviews;

	public ReviewAdapter(Activity context, int textViewResourceId, ArrayList<Review> items) {
		super(context, R.layout.reviewitem, items);
		this.context = context;
		this.reviews = items;
	}
	
	/* Returns a view for a Review.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			v = inflater.inflate(R.layout.reviewitem, null);
		}
		Review r = reviews.get(position);
		if (r != null) {
			TextView dt = (TextView) v.findViewById(R.id.review_String);
			TextView db = (TextView) v.findViewById(R.id.review_Rating); 
			if (dt != null) {
				dt.setText(r.getUsername() + ": " + r.comment());
			}
			if (db != null) {
				db.setText(context.getString(R.string.drinklabel_ratingcolon) + r.getRating());
			}
		}
		return v;
	}
}